import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

public class Tank {

	private static final int XSPEED = 5;
	private static final int YSPEED = 5;
	private static final int WIDTH = 30;
	private static final int HEIGHT = 30;

	Direction dir = Direction.STOP;
	boolean bU = false, bR = false, bD = false, bL = false;

	Direction ptDir = Direction.D;

	int x, y;
	
	private boolean good;
	private boolean live = true;

	public boolean isLive() {
		return live;
	}

	public void setLive(boolean live) {
		this.live = live;
	}

	TankWar tankWar;

	public Tank(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public Tank(int x, int y, Direction dir) {
		this(x, y);
		this.dir = dir;
	}

	public Tank(int x, int y, TankWar tankWar) {
		this(x, y);
		this.good =true;
		this.tankWar = tankWar;
	}

	public Tank(int x, int y, boolean good, TankWar tankWar){
		this(x, y, tankWar);
		this.good = good;
	}
	
	public void draw(Graphics g) {
		if(!isLive()) return;
		Color c = g.getColor();
		if(good == true) g.setColor(Color.RED);
		else g.setColor(Color.BLUE);
		g.fillOval(x, y, WIDTH, HEIGHT);
		g.setColor(c);

		switch (ptDir) {
		case U:
			g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH / 2, y);
			break;
		case RU:
			g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH, y);
			break;
		case R:
			g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH, y + HEIGHT / 2);
			break;
		case RD:
			g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH, y + HEIGHT);
			break;
		case D:
			g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH / 2, y + HEIGHT);
			break;
		case LD:
			g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x, y + HEIGHT);
			break;
		case L:
			g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x, y + HEIGHT / 2);
			break;
		case LU:
			g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x, y);
			break;
		case STOP:
			break;
		}

		move();
	}

	public void move() {
		switch (dir) {
		case U:
			y -= YSPEED;
			break;
		case RU:
			x += XSPEED;
			y -= YSPEED;
			break;
		case R:
			x += XSPEED;
			break;
		case RD:
			x += XSPEED;
			y += YSPEED;
			break;
		case D:
			y += YSPEED;
			break;
		case LD:
			x -= XSPEED;
			y += YSPEED;
			break;
		case L:
			x -= XSPEED;
			break;
		case LU:
			x -= XSPEED;
			y -= YSPEED;
			break;
		case STOP:
			break;
		}

		if (dir != Direction.STOP)
			ptDir = dir;

		if (x < 0)
			x = 0;
		if (y < 20)
			y = 20;
		if (x > TankWar.GAME_WIDTH - WIDTH)
			x = TankWar.GAME_WIDTH - WIDTH;
		if (y > TankWar.GAME_HEIGHT - HEIGHT)
			y = TankWar.GAME_HEIGHT - HEIGHT;
	}

	private void locateDir() {
		if (bR && !bD && !bL && !bU)
			dir = Direction.R;
		else if (bR && bD && !bL && !bU)
			dir = Direction.RD;
		else if (!bR && bD && !bL && !bU)
			dir = Direction.D;
		else if (!bR && bD && bL && !bU)
			dir = Direction.LD;
		else if (!bR && !bD && bL && !bU)
			dir = Direction.L;
		else if (!bR && !bD && bL && bU)
			dir = Direction.LU;
		else if (!bR && !bD && !bL && bU)
			dir = Direction.U;
		else if (bR && !bD && !bL && bU)
			dir = Direction.RU;
		else if (!bR && !bD && !bL && !bU)
			dir = Direction.STOP;
	}

	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		switch (key) {
		case KeyEvent.VK_RIGHT:
			bR = true;
			break;
		case KeyEvent.VK_DOWN:
			bD = true;
			break;
		case KeyEvent.VK_LEFT:
			bL = true;
			break;
		case KeyEvent.VK_UP:
			bU = true;
			break;
		}
		locateDir();
	}

	public void keyReleased(KeyEvent e) {

		int key = e.getKeyCode();
		switch (key) {
		case KeyEvent.VK_CONTROL:
			fire();
			break;
		case KeyEvent.VK_RIGHT:
			bR = false;
			break;
		case KeyEvent.VK_DOWN:
			bD = false;
			break;
		case KeyEvent.VK_LEFT:
			bL = false;
			break;
		case KeyEvent.VK_UP:
			bU = false;
			break;
		}
		locateDir();
	}

	private Missile fire() {
		int x = this.x + WIDTH / 2 - Missile.WIDTH / 2;
		int y = this.y + HEIGHT / 2 - Missile.HEIGHT / 2;
		Missile missile = new Missile(x, y, ptDir, this.tankWar);
		tankWar.missiles.add(missile);
		return missile;

	}
	
	public Rectangle getRectangle(){
		return new Rectangle(x, y, WIDTH, HEIGHT);
	}
	
	public boolean isHitByMissile(Missile missile){
		if(this.getRectangle().intersects(missile.getRectangle()) && this.isLive()){
			this.setLive(false);
			missile.setLive(false);
			tankWar.missiles.remove(missile);
			return true;
		}
		return false;
	}
}
